﻿#region Header

// Written by Tobias Lekman, 2008. For more information and contact details, see http://www.lekman.com.
// 
// File released under Microsoft Public License (Ms-PL). For full details see http://www.microsoft.com/opensource/licenses.mspx
// 
// Conditions and Limitations
// 
// (A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
// 
// (B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically.
// 
// (C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software.
// 
// (D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license.
// 
// (E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement. 

#endregion

#region Using Statements

using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.SharePoint;
using TypeMock.ArrangeActAssert;

#endregion

namespace CodePlex.Lekman.SharePoint.Testing
{
    /// <summary>
    /// Contains methods used with <see cref="TypeMock"/> mocking objects.
    /// </summary>
    public class MockTestFixture : IDisposable
    {
        #region Fields

        private bool isDisposed;

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="MockTestFixture"/> class.
        /// </summary>
        /// <param name="parent">The instance of the <see cref="SharePointTestFixture"/> that this class will be contained within.</param>
        public MockTestFixture(SharePointTestFixture parent)
        {
            ParentTestFixture = parent;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets an instance of the <see cref="SharePointTestFixture"/> that this class is contained within.
        /// </summary>
        public SharePointTestFixture ParentTestFixture{get;private set;}

        #endregion

        #region Methods

        /// <summary>
        /// Mocks calls to the <see cref="SPContext"/> object, replacing calls to <see cref="SharePoint"/> objects within <see cref="SPContext.Current"/> to the objects within a specific <see cref="SPWeb"/> object.
        /// </summary>
        /// <param name="web">The <see cref="SPWeb"/> to base the replaced <see cref="SPContext"/> on.</param>
        [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "Kept as instance methods for readability and usability.")]
        public void MockSharePointContext(SPWeb web)
        {
            Isolate.Fake.StaticMethods<SPContext>(Members.ReturnRecursiveFakes);
            Isolate.WhenCalled(() => SPContext.Current).WillReturn(SPContext.GetContext(web));
            Isolate.WhenCalled(() => SPContext.Current.Web).WillReturn(web);
            Isolate.WhenCalled(() => SPContext.Current.Site).WillReturn(web.Site);
        }

        /// <summary>
        /// Mocks calls to the <see cref="SPContext"/> object with a fake <see cref="SPContext"/> object.
        /// </summary>
        /// <returns>The fake <see cref="SPContext"/> object.</returns>
        [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "Kept as instance methods for readability and usability.")]
        public SPContext MockSharePointContext()
        {
            SPContext context = Isolate.Fake.Instance<SPContext>(Members.ReturnRecursiveFakes);
            Isolate.Fake.StaticMethods<SPContext>(Members.ReturnRecursiveFakes);
            Isolate.WhenCalled(() => SPContext.Current).WillReturn(context);
            return context;
        }

        #endregion

        #region Implementation of IDisposable

        /// <summary>
        /// Disposes the managed and unmanaged resources instantiated within the class.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Disposes the managed and unmanaged resources instantiated within the class.
        /// </summary>
        /// <param name="disposing">Determines whether managed resources should be disposed.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!isDisposed)
            {
                if (disposing)
                {
                    Isolate.CleanUp();
                }
            }
            isDisposed = true;
        }

        #endregion
    }
}